<?php
/**
 * The base Dig form adds a number of methods to Zend_Form
 *
 * @todo this class should be moved to the digCms module
 * 
 */
class Dig_Form_Abstract extends Zend_Form
{
    /**
     * @var string
     */
    protected $_instanceId = null;

    /**
     * The Dig Form constructor adds several items to your forms:
     *
     * * dig_referrer: this is the serialized request object
     * * digitalus_form_instance: a unique id which enables you to render multiple versions of the same form on a page
     *
     * It also registers the Dig form decorators and elements
     * 
     * @param array $options
     */
    public function __construct ($options = array())
    {
        parent::__construct($options);
        // by default set the action to itself and the method to post
        $this->setMethod('post');
        $id = $this->getAttrib('id');
        $this->_instanceId = empty($id) ? 'digitalus_form' : $id;
        $requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $referrer = $this->addElement('hidden', 'dig_referrer');
        $this->getElement('dig_referrer')->setValue(serialize($requestParams));
        $this->addElement('hidden', 'digitalus_form_instance');
        $this->getElement('digitalus_form_instance')->setValue($this->_instanceId);
        $this->getElement('digitalus_form_instance')->setDecorators(array('ViewHelper'));
        
        $this->addElementPrefixPath('Dig_Form_Decorator', APPLICATION_PATH . '/modules/dig/library/Form/Decorator', 'decorator');
        $this->addPrefixPath('Dig_Form_Element', APPLICATION_PATH . '/modules/dig/library/Form/Element/', 'element');
    }
    /**
     * this method first determines if the request is a post back
     * if it is it loads the post and validates the form
     * if it is valid then it returns true
     *
     * @return bool
     */
    public function validatePost ()
    {
        $frontController = Zend_Controller_Front::getInstance();
        if ($frontController->getRequest()->isPost() && $_POST['digitalus_form_instance'] == $this->_instanceId) {
            if ($this->isValid($_POST)) {
                return true;
            }
        }
        return false;
    }
    /**
     * This method loads the current form's values into a DigCms_Model_Item
     * 
     * @param string $itemClass the name of the class to instantiate
     *
     * @return DigCms_Model_Item
     */
    public function getValuesAsItem ($itemClass)
    {
        if(! $itemClass instanceof DigCms_Model_Item ) {
            $itemClass = new $itemClass();
        }
        $itemClass->loadArray($this->getValues());
        return $itemClass;
    }

    /**
     * This method saves a form to an instance of DigCms_Model_Item
     *
     * @param string $itemClass the name of the class to save the form to
     * 
     * @return boolean
     */
    public function saveToItem ($itemClass)
    {
        $item = $this->getValuesAsItem($itemClass);
        if ($item instanceof DigCms_Model_Item) {
            return $item->save();
        }
        return false;
    }
    /**
     * the populate method enables you to populate a form with an instance of DigCms_Item
     *
     * @param mixed $data
     */
    public function populate ($data)
    {
        if ($data instanceof DigCms_Model_Item) {
            $data = $data->toArray();
        }
        parent::populate($data);
    }

    /**
     * This method returns the request object from the page that posted the form
     * 
     * @return Zend_Controller_Request
     */
    public function getReferrer ()
    {
        $frontController = Zend_Controller_Front::getInstance();
        if ($frontController->getRequest()->isPost()) {
            return unserialize($this->getValue('dig_referrer'));
        }
        return false;
    }
}
?>